import nProgress from "nprogress";
import { createWebHashHistory, createRouter } from "vue-router";
import Home from "@/views/home/index.vue";
import School from "@/views/school/index.vue";
import MallList from "@/views/mall/list.vue";

import MallSort from "@/views/mall/sort.vue";
import CourseSort from "@/views/course/courseType.vue";
import CourseList from "@/views/course/list.vue";
import Promotion from "@/views/promotion/index.vue";
import Login from "@/views/login/index.vue";
import { isMiniProgram } from "@/utils/mini-program";
import wx from "weixin-js-sdk";

const routes: any[] = [
  {
    path: "/",
    name: "Home",
    component: Home,
    children: [
      {
        path: "",
        name: "School",
        component: School,
      },
      {
        path: "/mall",
        name: "Mall",
        component: MallSort,
      },
      {
        path: "/course",
        name: "CourseList",
        component: CourseSort,
        // component: CourseList,
      },
      {
        path: "/promotion",
        name: "Promotion",
        component: Promotion,
      },
      {
        path: "/courseList/:type",
        name: "courseList",
        props: true,
        component: CourseList
      },
    ],
  },

  {
    path: "/news/home",
    name: "NewsHome",
    component: () => import("@/views/news/new-home.vue"),
  },
  {
    path: "/news/list",
    name: "NewsList",
    component: () => import("@/views/news/list.vue"),
  },
  {
    path: "/school/about",
    name: "SchoolAbout",
    component: () => import("@/views/school/about.vue"),
  },
  {
    path: "/school/coach",
    name: "SchoolCoach",
    component: () => import("@/views/school/coach.vue"),
  },
  {
    path: "/school/guide",
    name: "SchoolGuide",
    component: () => import("@/views/school/guide.vue"),
  },
  {
    path: "/school/guide/:guideId",
    name: "SchoolGuideDetail",
    props: true,
    component: () => import("@/views/school/guide-detail.vue"),
  },
  {
    path: "/school/environment",
    name: "SchoolEnvironment",
    component: () => import("@/views/school/environment.vue"),
  },
  {
    path: "/news/:newsId",
    name: "NewsDetail",
    props: true,
    component: () => import("@/views/news/detail.vue"),
  },

  {
    path: "/course/:id",
    name: "CourseDetail",
    props: true,
    component: () => import("@/views/course/detail.vue"),
  },
  {
    path: "/mall/:spuId",
    name: "MallDetail",
    props: true,
    component: () => import("@/views/mall/detail.vue"),
  },
  
  {
    path: "/mallList/:type",
    name: "mallList",
    props: true,
    component: () => import("@/views/mall/list.vue"),
  },

  {
    path: "/course/article/:id",
    name: "CourseArticle",
    props: true,
    component: () => import("@/views/course/article.vue"),
  },
  {
    path: "/course/live",
    name: "CourseLive",
    props: true,
    component: () => import("@/views/course/live.vue"),
  },
  {
    path: "/buy/success",
    name: "BuySuccess",
    component: () => import("@/views/buy/success.vue"),
  },
  {
    path: "/buy/:skuId",
    name: "Buy",
    props: true,
    component: () => import("@/views/buy/index.vue"),
  },
  {
    path: "/order",
    name: "OrderList",
    component: () => import("@/views/order/list.vue"),
  },
  {
    path: "/order/:orderNo",
    name: "OrderDetail",
    props: true,
    component: () => import("@/views/order/detail.vue"),
  },
  {
    path: "/publish-msg",
    name: "PublishMsg",
    component: () => import("@/views/publish/msg/index.vue"),
  },
  {
    path: "/publish-detail",
    name: "PublishDetail",
    component: () => import("@/views/publish/detail/index.vue"),
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/test",
    name: "Test",
    component: () => import("@/views/test/index.vue"),
  },
  {
    path: "/achievement",
    name: "Achievement",
    component: () => import("@/views/promotion/achievement.vue"),
  },
  {
    path: "/certification",
    name: "Certification",
    component: () => import("@/views/certification/certification.vue"),
  },
  {
    path: "/invite-user",
    name: "InviteUser",
    component: () => import("@/views/promotion/invite-user.vue"),
  },
  {
    path: "/clue",
    name: "Clue",
    component: () => import("@/views/promotion/clue.vue"),
  },
  {
    path: "/history-clue",
    name: "HistoryClue",
    component: () => import("@/views/promotion/history-clue.vue"),
  },
  {
    path: "/member-advertising",
    name: "memberAdvertising",
    component: () => import("@/views/promotion/member-advertising.vue"),
  },
  {
    path: "/poster/:type",
    name: "Poster",
    props: true,
    component: () => import("@/views/promotion/poster.vue"),
  },
  {
    path: "/practice/practice",
    name: "PracticeExam",
    component: () => import("@/views/practice/practice.vue"),
  },
  {
    path: "/practice/result/:paperId",
    name: "PracticeResult",
    props: true,
    component: () => import("@/views/practice/result.vue"),
  },
  {
    path: "/practice/history/:mode",
    name: "PracticeHistory",
    props: true,
    component: () => import("@/views/practice/history.vue"),
  },
  {
    path: "/practice/exam-history",
    name: "ExamHistory",
    component: () => import("@/views/practice/exam-history.vue"),
  },
  {
    path: "/practice/exam-strategy",
    name: "ExamStrategy",
    component: () => import("@/views/practice/exam-strategy.vue"),
  },
  {
    path: "/practice/:type",
    name: "Practice",
    props: true,
    component: () => import("@/views/practice/index.vue"),
  },
  {
    path: "/contact",
    name: "Contact",
    component: () => import("@/views/contact/index.vue"),
  },
  {
    path: "/information",
    name: "Information",
    component: () => import("@/views/information/index.vue"),
  },
  {
    path: "/information/text-content",
    name: "TextContent",
    component: () => import("@/views/information/text-content.vue"),
  },
  {
    path: "/information/video-content",
    name: "VideoContent",
    component: () => import("@/views/information/video-content.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(import.meta.env.VITE_BASE_PATH as string),
  routes,
});

router.beforeEach((to, from, next) => {
  if (isMiniProgram()) {
    if (to.path === "/contact") {
      wx.miniProgram.navigateTo({ url: "/pages/contact/contact" });
      console.log("miniProgram navigateTo");
      return;
    }
  }

  if (typeof to.matched[0]?.components?.default === "function") {
    nProgress.start();
  }
  next();
});

router.beforeResolve((to, from, next) => {
  nProgress.done();
  next();
});

export default router;
